Ext.define('App.system.codeForm', {
    extend: 'Ext.window.Window',
    alias: 'widget.codeForm',
    initComponent: function () {

        var form = Ext.widget('form', {
            frame: false,
            border: true,
            split: true,
            autoScroll: true,
            bodyPadding: 5,
            bodyBorder: true,
            fieldDefaults: {
                labelWidth: 75,
                labelAlign: 'right',
                msgTarget: 'side'
            },
            defaults: {
                anchor: '100%'
            },
            items: [
                { xtype: 'hidden',
                    name: "id",
                    dataIndex: "id"
                },
                {   xtype: 'hidden',
                    name: "spareProperty",
                    dataIndex: "spareProperty"
                },
                { xtype: 'textfield',
                    name: "name",
                    dataIndex: "name",
                    fieldLabel: "编码名称",
                    allowBlank: false
                },
                {    xtype: 'textfield',
                    name: "description",
                    dataIndex: "description",
                    fieldLabel: "描述",
                    allowBlank: true
                },
                {     xtype: 'checkbox', name: "isTree",
                    dataIndex: "isTree",
                    fieldLabel: "编码树",
                    inputValue: '1'
                }
            ]
        });
        Ext.apply(this, {
            title: '新增',
            layout: 'fit',
            height: 300,
            width: 500,
            modal: true,
            constrain: true,
            closeAction: 'hide',
            buttonAlign: 'center',
            buttons: [
                {
                    text: "保存",
                    handler: function () {
                        //this.up(‘window’)获取了顶级的window组件。接着使用down()方法获取了window的子组件form组件，最后使用getForm()来获取整个form。
                        var form = this.up('codeForm').down('form');
                        if (form.getForm().isValid()) {
                            form.getForm().submit({
                                url: getRootPath() + '/codeController/save.action',
                                scope: form,
                                success: function (form, action) {
                                    this.up('codeForm').close();
                                    Ext.getCmp('codeGrid').getStore().reload();
                                },
                                failure: function (form, action) {
                                    if (action.result.message == undefined)
                                        Ext.Msg.alert('错误', '网络连接失败.');
                                    else
                                        Ext.Msg.alert('错误', action.result.message);
                                },
                                waitMsg: '保存数据...'
                            });
                        } else {
                            Ext.Msg.alert('提示', '表单含有无效或空数据');
                        }
                    }
                },
                {
                    text: "关闭",
                    handler: function () {
                        this.up('codeForm').close();
                    }
                }
            ],
            items: form
        });

        this.callParent(arguments);
    }
});